package com.lixueju.security.box.web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixueju.security.box.core.model.User;
import com.lixueju.security.box.core.model.request.*;
import com.lixueju.security.box.core.properties.SecurityBoxConstants;
import com.lixueju.security.box.web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户基础表，存储用户基本信息 前端控制器
 * </p>
 *
 * @author lixueju
 * @since 2019-11-13
 */
@RestController
@RequestMapping(value = "/security/box/user", produces = "application/json;charset=utf-8")
public class UserController extends SecurityBoxBaseController {

    @Autowired
    private IUserService userService;



    @GetMapping("/userList")
    public String userList(User user) {
        Page<User> page = getPage(false);
        return callbackSuccess(userService.selectUserPage(page, user));
    }

    /**
     * 修改用户的角色信息
     */
    @PostMapping("/updateUserRole")
    public String updateUserRole(@RequestBody UpdateUserRequest updateUserRequest) {
        return callbackSuccess(userService.updateUserRole(updateUserRequest));
    }

    /**
     * 新增用户
     */
    @PostMapping("/addUser")
    public String addUser(@RequestBody User user) {
        return callbackSuccess(userService.addUser(user));
    }

    /**
     * 修改用户
     */
    @PostMapping("/updateUser")
    public String updateUser(@RequestBody User user) {
        return callbackSuccess(userService.updateUser(user));
    }

    @GetMapping("/userInfo")
    public String userInfo(User user) {
        return callbackSuccess(securityBoxUtils.getUserToken(request.getHeader("token")));
    }
    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/logOut")
    public String logOut() {
        return callbackSuccess(securityBoxUtils.deleteUserToken(request.getHeader(SecurityBoxConstants.DEFAULT_TOKEN_KEY)));
    }

    /**
     * 绑定手机号
     */
    @PostMapping("/bindMobile")
    public String bindMobile(@RequestBody BindMobileRequest bindMobileRequest) {
        return callbackSuccess(userService.bindMobile(bindMobileRequest,getUserId(),request));
    }

    /**
     * 微信小程序绑定手机号
     */
    @PostMapping("/bindWeixinMobile")
    public String bindWeixinMobile(@RequestBody WeixinDecodeLoginRequest weixinDecodeLoginRequest) {
        return callbackSuccess(userService.bindWeixinMobile(weixinDecodeLoginRequest,getUserId(),request));
    }

    /**
     * 根据code绑定微信
     */
    @PostMapping("/bindWeixin")
    public String bindWeixin(@RequestBody WeixinLoginRequest weixinLoginRequest) {
        return callbackSuccess(userService.bindWeixin(weixinLoginRequest,getUserId(),request));
    }

    /**
     * 刷新token
     */
    @PostMapping("/refreshToken")
    public String refreshToken() {
        return callbackSuccess(true);
    }

    @PostMapping("/upPassword")
    public String upPassword(@RequestBody upPasswordRequest upPasswordRequest) {
        return callbackSuccess(userService.upPassword(upPasswordRequest,getUserId()));
    }

    @PostMapping("/upPasswordByMobile")
    public String upPasswordByMobile(@RequestBody upPasswordRequest upPasswordRequest) {
        return callbackSuccess(userService.upPasswordByMobile(upPasswordRequest));
    }

}

